package com.film.dao;

import com.film.entity.*;
import com.film.util.DBTemplate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HustleLr
 * @version 1.0.0
 * @date 2023/12/13 18:07
 * @describe ReservationDao
 */
public class ReservationDao extends DBTemplate {

    public List<Reservation> getReservations() {
        String sql = "select r.reservation_id, u.`name` as userName, ms.session_time, r.seat_number, ch.location, " +
                "ch.`name` as cineName, m.title as movieName from reservations r " +
                "LEFT JOIN users u on r.user_id=u.user_id " +
                "LEFT JOIN moviesessions ms on ms.session_id=r.session_id " +
                "left join cinemahalls ch on ch.hall_id=ms.hall_id " +
                "left join movies m on m.movie_id=ms.movie_id and ch.hall_id=ms.hall_id";
        return (List<Reservation>) executeQuery(sql, null);
    }

    @Override
    protected Object processResultSet(ResultSet rs) throws SQLException {
        List<Reservation> reservationList = new ArrayList<Reservation>();
        while (rs.next()) {
            String reservationId = rs.getString("reservation_id");
            String userName = rs.getString("userName");
            LocalDateTime sessionTime = rs.getTimestamp("session_time").toLocalDateTime();
            String seatNumber = rs.getString("seat_number");
            String location = rs.getString("location");
            String cineName = rs.getString("cineName");
            String movieName = rs.getString("movieName");
            Reservation reservation = new Reservation();
            reservation.setReservationId(reservationId);
            User user = new User();
            user.setName(userName);
            MovieSession movieSession = new MovieSession();
            movieSession.setSessionTime(sessionTime);
            Movie movie = new Movie();
            movie.setTitle(movieName);
            movieSession.setMovie(movie);
            CinemaHall cinemaHall = new CinemaHall();
            cinemaHall.setName(cineName);
            cinemaHall.setLocation(location);
            movieSession.setCinemaHall(cinemaHall);
            reservation.setUser(user);
            reservation.setMovieSession(movieSession);
            reservation.setSeatNumber(seatNumber);
            reservationList.add(reservation);
        }
        return reservationList;
    }

    @Override
    protected PreparedStatement createPreparedStatement(Connection conn, String sql, List<Object> params) throws SQLException {
        PreparedStatement sta = conn.prepareStatement(sql);
        if(null!=params&&!params.isEmpty()) {
            for (int i = 0; i < params.size(); i++) {
                sta.setObject(i + 1, params.get(i));
            }
        }
        return sta;
    }

    public void saveReservations(Reservations reservation) {
        String sql = "insert into reservations(user_id, session_id, seat_number) values(?,?,?)";
        List<Object> param =new ArrayList<>();
        param.add(reservation.getUserId());
        param.add(reservation.getSessionId());
        param.add(reservation.getSeatNumber());
        executeUpdate(sql, param);
    }
}
